public class MyQueue {

    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode first = null;
    public ListNode last = null;

    public int usedSize = 0;

    public void offer(int val) {
        ListNode node=new ListNode(val);
        if(isEmpty()){
            first=last=node;
            usedSize++;
            return;
        }
        node.prev=last;
        last.next=node;
        last=last.next;
        usedSize++;
    }

    public int poll() {
        if(isEmpty()){
            return -1;
        }
        int val= first.val;
        if(usedSize==1){
            first=last=null;
            return val;
        }
        first.next.prev=null;
        first=first.next;
        usedSize--;
        return val;
    }

    public int peek() {
        if(isEmpty()){
            return -1;
        }
        return first.val;
    }

    public boolean isEmpty() {
        return usedSize==0;
    }

}

